#include "stdio.h"
#include "stdlib.h"
#include "string.h"

struct course{
    int number;
    char *name;
    char *size;
    int time[2];
    char *teacher;
};

struct choose{
    int number;
    char *name;
    int id;
    struct choose *next;
};

int amount[4]={0,0,0,0};

struct course information[4];

void add_course(struct course *a);
void find_course(struct course *b);
struct choose * creat_table();
void find_choose(struct choose *d);
void choose(struct choose *c);
void drop_choose(struct choose *e);
void save(struct course *m,struct choose *n);

int main(){
    printf("1.增加课程\n2.查询课程\n3.查询选课信息\n4.选课\n5.退选\n6.保存课程及其选课学生信息\n");
    int a;
    struct choose *head;
    head=creat_table();
    while (scanf("%d",&a)!=EOF) {
        switch (a) {
            case 1:add_course(information);break;
            case 2:find_course(information);break;
            case 3:find_choose(head);break;
            case 4:choose(head);break;
            case 5:drop_choose(head);break;
            case 6:save(information, head);break;
            default:printf("no such opreation");break;
        }
    }
}

void add_course(struct course *a){
    for (int i=0; i<4; i++) {
        scanf("%d %s %s %d %d %s\n",&((a+i)->number),(a+i)->name,(a+i)->size,&((a+i)->time[0]),&((a+i)->time[1]),(a+i)->teacher);
    }
}

void find_course(struct course *b){
    int k=0;
    char *aim;
    struct course sort[10];
    scanf("%s",aim);
    for (int i=0; i<4; i++) {
        if(strcmp(aim,(b+i)->name)==0){
            sort[k]=*(b+i);
            k++;
        }
    }
    for (int j=0; j<k; j++) {
        for (int n=0; n<k-j; n++) {
            if(sort[n].number>sort[n+1].number){
                struct course temp;
                temp=sort[n];
                sort[n]=sort[n+1];
                sort[n+1]=temp;
            }
        }
    }
    for (int j=0; j<k+1; j++) {
        printf("%d %s %s %d:%d %s\n",sort[j].number,sort[j].name,sort[j].size,sort[j].time[0],sort[j].time[1],sort[j].teacher);
    }
}

struct choose * creat_table(){
    struct choose *head,*pb;
    pb=(struct choose *)malloc(sizeof(struct choose));
    head=pb;
    pb->next=NULL;
    return head;
}

void find_choose(struct choose *d){
    int number;
    scanf("%d",&number);
    while (d!=NULL) {
        if (number==d->number) {
            printf("%d %s %d\n",d->number,d->name,d->id);
        }
        d=d->next;
    }
}

void choose(struct choose *c){
    int number;
    scanf("%d",&number);
    for (int i=0; i<4; i++) {
        if (number==information[i].number) {
            if(strcmp(information[i].size,"big")==0){
                if (amount[i]<5) {
                    while (c->next!=NULL) {
                        c=c->next;
                    }
                    struct choose *pb;
                    pb=(struct choose *)malloc(sizeof(struct choose));
                    scanf("%d %s %d\n",&(pb->number),pb->name,&(pb->id));
                    c->next=pb;
                    pb->next=NULL;
                    amount[i]+=1;
                }
                else{
                    printf("教学班已满");
                }
            }
            else{
                if (amount[i]<3) {
                    while (c->next!=NULL) {
                        c=c->next;
                    }
                    struct choose *pb;
                    pb=(struct choose *)malloc(sizeof(struct choose));
                    scanf("%d %s %d\n",&(pb->number),pb->name,&(pb->id));
                    c->next=pb;
                    pb->next=NULL;
                    amount[i]+=1;
                }
                else{
                    printf("教学班已满");
                }
            }
        }
    }
}

void drop_choose(struct choose *e){
    struct choose *pf = NULL,*pb;
    int number;
    char *name;
    pf=pb=e;
    scanf("%d %s",&number,name);
    while (pb!=NULL) {
        if ((pb->number==number)&&(strcmp(pb->name, name)==0)) {
            pf->next=pb->next;
            free(pb);
        }
        else{
            pf=pb;
            pb=pb->next;
        }
    }
}

void save(struct course *m,struct choose *n){
    FILE *fp1;
    int i=1;
    while (n!=NULL) {
        n=n->next;
        i++;
    }
    if ((fp1=fopen("/Users/zhuyufeng/Desktop/course.txt", "w+"))!=0) {
        fwrite(m, sizeof(struct course), 4, fp1);
        fwrite(n, sizeof(struct choose), i-1, fp1);
    }
    fclose(fp1);
}